﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;

namespace eProcurement.DAL
{
    public enum ProjectState
    {
        OPEN = 1,
        CLOSED = 2,
        CANCELLED = 3,
        EVALUATION = 4
    }

    partial class Purchaser
    {
        public int NumberOpenProjects
        {
            get
            {
                eProcurementModelContainer context = new eProcurementModelContainer();
                string openstate = ProjectState.OPEN.ToString();
                var result = from i in context.AppUsers.OfType<Purchaser>()
                             from e in i.Projects
                             where EntityFunctions.TruncateTime( e.CloseDate ) > EntityFunctions.TruncateTime( DateTime.Now )
                             && e.State == openstate
                             select i;
                
                return result.Count();
            }
        }

        public List<Project> OpenProjects
        {
            get
            {
                using ( eProcurementModelContainer context = new eProcurementModelContainer() )
                {
                    string openstate = ProjectState.OPEN.ToString();
                    var result = from i in context.Projects
                                 where EntityFunctions.TruncateTime( i.CloseDate ) > EntityFunctions.TruncateTime( DateTime.Now )
                                 && i.State == openstate
                                 && i.PurchaserId == this.PurchaserId
                                 select i;

                    return result.ToList();
                }
            }
        }
    }

    partial class Project
    {
        public int NumberBidders
        {
            get
            {
                return this.Bids.Count();
            }
        }
    }
}

